#include <COMMON_HEADER/common_header.h>
/*
 * Copyright (c) 2016-2020, Chengdu RST technology co., ltd
 *
 * Change Logs:
 * Date           Author       Notes
 * 2020-12-17     shijiabao       the first version
 */

/**
 * chip flash 读flash数据功能
  *  函数功能：读片上flash数据功能
 */
void chip_flash_read(uint32_t ADDR,rt_uint8_t *data_buf,uint32_t size)
{
    rt_memset(data_buf,0,sizeof(data_buf));
    stm32_flash_read(ADDR,data_buf,size);
}

/**
 * chip flash 写片上 flash数据功能
  *  函数功能：写片上 flash数据功能
 */
void chip_flash_write(uint32_t ADDR,rt_uint8_t *data_buf,uint32_t size)
{
    /* 页擦除以2KB每页为单位 */
    stm32_flash_erase(ADDR,size);
    stm32_flash_write(ADDR,data_buf,size);
}

/**
 * chip flash 读flash数据参数的功能
  *  函数功能：读片上flash数据及参数的功能
  *  return 0:参数为空
  *  return 1:成功读取参数
 */
rt_uint8_t chip_flash_configuretion_read(uint32_t ADDR,char *data_buf)
{
    rt_uint8_t data_buffer[0x20] = {0};
    rt_memset(data_buf,0,sizeof(data_buf));
    stm32_flash_read(CONFIGURE_FLASH_ADDR+ADDR, data_buffer, 0x20);
    if(data_buffer[0] == 0xff)
    {
        return 0;
    }
    else
    {
        for(int i = 0;i<data_buffer[0];i++)
        {
            data_buf[i] = data_buffer[i+1];
        }
    }
    return 1;
}
